class GeozonePolygonController {

    def index = {redirect(action: list, params: params)}

    // the delete, save and update actions only accept POST requests
    def allowedMethods = [delete: 'POST', save: 'POST', update: 'POST']

    def list = {
        if (!params.max) params.max = 10
        [geozonePolygonList: GeozonePolygon.list(params)]
    }

    def show = {
        def geozonePolygon = GeozonePolygon.get(params.id)

        if (!geozonePolygon) {
            flash.message = "GeozonePolygon not found with id ${params.id}"
            redirect(action: list)
        }
        else {return [geozonePolygon: geozonePolygon]}
    }

    def delete = {
        def geozonePolygon = GeozonePolygon.get(params.id)
        if (geozonePolygon) {
            geozonePolygon.delete()
            flash.message = "GeozonePolygon ${params.id} deleted"
            redirect(action: list)
        }
        else {
            flash.message = "GeozonePolygon not found with id ${params.id}"
            redirect(action: list)
        }
    }

    def edit = {
        def geozonePolygon = GeozonePolygon.get(params.id)

        if (!geozonePolygon) {
            flash.message = "GeozonePolygon not found with id ${params.id}"
            redirect(action: list)
        }
        else {
            return [geozonePolygon: geozonePolygon]
        }
    }

    def update = {
        def geozonePolygon = GeozonePolygon.get(params.id)
        if (geozonePolygon) {
            geozonePolygon.properties = params
            if (!geozonePolygon.hasErrors() && geozonePolygon.save()) {
                flash.message = "GeozonePolygon ${params.id} updated"
                redirect(action: show, id: geozonePolygon.id)
            }
            else {
                render(view: 'edit', model: [geozonePolygon: geozonePolygon])
            }
        }
        else {
            flash.message = "GeozonePolygon not found with id ${params.id}"
            redirect(action: edit, id: params.id)
        }
    }

    def create = {
        def geozonePolygon = new GeozonePolygon()
        geozonePolygon.properties = params
        return ['geozonePolygon': geozonePolygon]
    }

    def save = {
        def geozonePolygon = new GeozonePolygon(params)
        def td = params.tracedata
        def lines = td?.split("\r\n");
        def points = []
        lines.each {
            line ->
            def latlon = line.split(",")
            def lat = latlon[0]
            def lon = latlon[1]
            points.add(new Point("latitude": lat, "longitude": lon,"zoneType":"Polygon"))
        }

        points.each {
            point ->
            geozonePolygon.addToPoints(point)
        }

        if (!geozonePolygon.hasErrors() && geozonePolygon.save()) {
            flash.message = "GeozonePolygon ${geozonePolygon.id} created"
            redirect(action: show, id: geozonePolygon.id)
        } else {
            flash.message = "GeozonePolygon ${geozonePolygon.id} failed"
            render(view: 'create', model: [geozonePolygon: geozonePolygon])
        }
    }
}